package com.shiikong_memory.yez.Arknights;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.shiikong_memory.yez.Arknights.ArknightsData.LotteryData;
import com.shiikong_memory.yez.MySQLAPI.DaoImpl.LotteryDataServer;
import com.shiikong_memory.yez.MySQLAPI.DaoImpl.SklandServer;
import com.shiikong_memory.yez.MySQLAPI.JavaBean.LotteryDataBean;
import com.shiikong_memory.yez.MySQLAPI.JavaBean.SklandBean;
import love.forte.simbot.component.kook.event.KookChannelMessageEvent;
import love.forte.simbot.kook.api.message.SendChannelMessageApi;
import love.forte.simbot.quantcat.common.annotations.ContentTrim;
import love.forte.simbot.quantcat.common.annotations.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;

@Component
public class GetLotteryData {
    @Autowired
    SklandServer sklandServer;

    @Autowired
    private SklandAPI sklandAPI;

    @Autowired
    LotteryDataServer lotteryDataServer;

    /**
     * 明日方舟抽卡数据监听器
     * @param event 监听频道事件
     * @return 异步发送消息
     */
    @Listener
    @ContentTrim
    public CompletableFuture<?> GetData(KookChannelMessageEvent event){
        if (event.getSourceEvent().getContent().matches("^获取明日方舟抽卡数据$")){
            SklandBean sklandBean = sklandServer.getByKookId(event.getAuthor().getId().toString());
            if (sklandBean == null){
                return event.replyAsync("未登录鹰角通行证，无法查询数据，请先登录鹰角通行证");
            }
            String id = sklandBean.getArknightsToken();
            if (!sklandAPI.verifyToken(id)){
                return event.replyAsync("鹰角通行证Token不合法请重新获取");
            }
            JSONArray array = new JSONArray();
            LotteryDataBean byChatChannelId = lotteryDataServer.getByChatChannelId(event.getAuthor().getId().toString());
            Long ts = null;
            if (byChatChannelId != null) ts = byChatChannelId.getTs();
            for (int i=1;;i++){
                JSONObject lotteryData = null;
                lotteryData = sklandAPI.getLotteryData(id, i);
                JSONArray jsonArray = lotteryData.getJSONObject("data").getJSONArray("list");
                if (jsonArray.isEmpty()) break;
                for (int j=0;j<jsonArray.size();j++){
                    JSONObject jsonObject = jsonArray.getJSONObject(j);
                    if (ts != null) if (jsonObject.getLong("ts") <= ts) break;
                    array.add(jsonObject);
                }
            }
            LotteryData lotteryData;
            if (byChatChannelId == null){
                if (array.isEmpty()){
                    return event.replyAsync("未查询到抽奖数据，可能是b服账号，目前不支持b服账号");
                }
                lotteryData = new LotteryData(array);
                lotteryDataServer.addLotteryData(event.getAuthor().getId().toString(), lotteryData.getTs(), lotteryData.getJson(), lotteryData.getAllSize());
            }else {
                lotteryData = new LotteryData(byChatChannelId);
                lotteryData.addData(array);
                lotteryDataServer.updateByKookId(event.getAuthor().getId().toString(), lotteryData.getTs(), lotteryData.getJson(), lotteryData.getAllSize());
            }
            return event.getContent().sendAsync(SendChannelMessageApi.create(10,event.getSourceEvent().getTargetId(), lotteryData.getKMarkdown()));
        }
        return null;
    }

    /***
     * 定时刷新抽卡数据信息
     */
    @Async
    @Scheduled(cron="0 0 6 * * ?")
    public void stratAutoCheckIn(){
        for (SklandBean sklandBean:sklandServer.getAll()){
            String id = sklandBean.getArknightsToken();
            if (!sklandAPI.verifyToken(id)){
                System.err.println(sklandBean.getArknightsToken()+"的鹰角通行证Token不合法请重新获取");
                continue;
            }
            JSONArray array = new JSONArray();
            LotteryDataBean byChatChannelId = lotteryDataServer.getByChatChannelId(sklandBean.getKookId());
            Long ts = null;
            if (byChatChannelId != null) ts = byChatChannelId.getTs();
            for (int i=1;;i++){
                JSONObject lotteryData = null;
                lotteryData = sklandAPI.getLotteryData(id, i);
                JSONArray jsonArray = lotteryData.getJSONObject("data").getJSONArray("list");
                if (jsonArray.isEmpty()) break;
                for (int j=0;j<jsonArray.size();j++){
                    JSONObject jsonObject = jsonArray.getJSONObject(j);
                    if (ts != null) if (jsonObject.getLong("ts") <= ts) break;
                    array.add(jsonObject);
                }
            }
            LotteryData lotteryData;
            if (byChatChannelId == null){
                lotteryData = new LotteryData(array);
                lotteryDataServer.addLotteryData(sklandBean.getKookId(), lotteryData.getTs(), lotteryData.getJson(), lotteryData.getAllSize());
            }else {
                lotteryData = new LotteryData(byChatChannelId);
                lotteryData.addData(array);
                lotteryDataServer.updateByKookId(sklandBean.getKookId(), lotteryData.getTs(), lotteryData.getJson(), lotteryData.getAllSize());
            }
        }
    }
}
